/*************************************************************
 * File: Condition.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Feb 21, 2013
 * Description: 
 ************************************************************/

package snow.core.persistence;

import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;

/**
 * 构造查询条件
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public class Condition {
	
	public static boolean IS_NULL = true;
	public static boolean NOT_NULL = false;

	private String name;
	private Operator operator;

	private Object value;
	private Object secondValue;

	private boolean noValue;
	private boolean singleValue;
	private boolean betweenValue;
	private boolean listValue;

	public Condition(String name, BinaryOperator operator, Object value) {
		this.name = name;
		this.operator = operator;
		this.value = value;
		this.singleValue = true;
	}

	public Condition(String name, boolean isNull) {
		this.name = name;
		this.noValue = true;
		this.operator = isNull ? Operator.IS_NULL : Operator.NOT_NULL;
	}

	public Condition(String name, Collection<?> value) {
		this.name = name;
		this.value = value;
		this.listValue = true;
		this.operator = Operator.IN;
	}

	public Condition(String name, Object value, Object secondValue) {
		this.name = name;
		this.value = value;
		this.secondValue = secondValue;
		this.operator = Operator.BETWEEN;
		this.betweenValue = true;
	}

	public Condition(String name, Object[] value) {
		this(name, value == null ? (Collection<?>) null : Arrays.asList(value));
	}

	public String getName() {
		return this.name;
	}

	public Operator getOperator() {
		return this.operator;
	}

	public Object getSecondValue() {
		return this.secondValue;
	}

	public Object getValue() {
		return this.value;
	}

	public boolean isBetweenValue() {
		return this.betweenValue;
	}

	public boolean isListValue() {
		return this.listValue;
	}

	public boolean isNoValue() {
		return this.noValue;
	}

	public boolean isSingleValue() {
		return this.singleValue;
	}

	public boolean isValid() {
		if (StringUtils.isBlank(this.name)) {
			return false;
		}
		if (this.operator == null) {
			return false;
		}
		if (this.noValue) {
			if (!(this.operator == Operator.IS_NULL || this.operator == Operator.NOT_NULL) || this.value != null) {
				return false;
			}
			return true;
		} else if (this.betweenValue) {
			if (Operator.BETWEEN != this.operator || this.value == null || this.secondValue == null) {
				return false;
			}
			return true;
		} else if (this.listValue) {
			if (Operator.IN != this.operator || !(this.value instanceof Collection)) {
				return false;
			}
			return true;
		} else if (this.singleValue) {
			if (!(operator instanceof BinaryOperator) || this.value == null) {
				return false;
			}
			return true;
		}
		return false;
	}
}
